import { TeamOrder, Arms } from "../../config/play_def"
import { CommonBuff } from "../../config/role_def";

cc.Class({
    extends: cc.Component,

    properties: {
        // 测试用到时候删
        order_label: cc.Label,  // 命令子弹
        model_node: cc.Node,    // 模型节点
        // 正式排序
        order: 0,
        controling:false,       // 是否操纵中
        out_status:false,       // 已经出战
        lastAni:'',             // 最后动画
        cover_model:sp.Skeleton,// 遮罩模型 

    },
    initInfo(order){
        this.order = order
        // this.order_label.string = TeamOrder[order - 1]
        this.node.name = TeamOrder[order - 1] + ''
        this.model = this.model_node.getComponent(sp.Skeleton)
        this.role_id = null
        this.model.setCompleteListener((trackEntry) => {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            this.listenAniTag(animationName)
        });
        // order 是 12345 也就是顺序队列
        // TeamOrder 是 53124 就是user部分的队列
    },    
    listenAniTag(ani){
        switch(ani){
            default:
                this.cover_model.node.active = false
            break
        }
    },
    onDestroy(){
        this.model.setCompleteListener(null)
    },
    // 此处更新战斗脚本
    updateInfo(param){
        this.model_node.active = true
        let role_lv = param.equips[param.hand]
        if(!role_lv){
            for(let i = 0;i < param.equips.length ;i++){
                if(!role_lv){
                    role_lv = param.equips[i]
                }
                if(param.equips[i] < role_lv){
                    role_lv = param.equips[i]
                }
            }
        }
        let tower_info = he.db.getDataByArgs("Equip",{ equip: param.hand, level: param.equips[param.hand] })
        let role_info = he.db.getDataByArgs("Role", { roleId: param.id, level: role_lv})
        this.role_id = param.id
        // 按照序号添加皮肤
        // 之后需要删除
        this.model.skeletonData = he.load.get(role_info.model,sp.SkeletonData,"role")
        this.cover_model.defaultSkin = role_info.debutSkin
        this.cover_model.setSkin(role_info.debutSkin)
        this.reScript(tower_info.script)
        this.out_status = false
        if(this.tower_script){
            this.tower_script.initInfo(tower_info,role_info)
            this.tower_script.IdelAction()
        }else{
            he.error(tower_info.script,"脚本未绑定")
        }
    },
    // 换出角色
    swapOut(backTime){
        this.cover_model.node.active = true
        this.cover_model.defaultAnimation = 'out'
        this.cover_model.setAnimation(0,'out',false)
        this.out_status = true
        this.model_node.active = false  // 这里改一手发光动画
        
        let role_param = gg.data.getRoleByRoleID(this.role_id) 
        if(role_param){
            let swap_buff = {
                key : CommonBuff.SwapRole,                                                          // 更换时间
                value: 1,                                                                           // 需要值
                role : `${role_param.hand}/${role_param.id}/${role_param.equips[role_param.hand]}`, // 换入值
                order: this.order,                                                                  // 脚本位置
                time : backTime                                                                     // 返回时间
            }
            gg.control.addBuff(swap_buff)            // 加入换人Buff            
        }else{
            he.error("角色报空拉，检查容错问题")
        } 
    },
    // 换入角色
    swapIn(param){
        this.cover_model.node.active = true
        this.cover_model.defaultAnimation = 'in'
        this.cover_model.setAnimation(0,'in',false)
        let info_list = param.split('/')
        // 装备编号，角色编号, 当前等级
        let base_lv = 0
        if(info_list[2]){
            base_lv = parseInt(info_list[2])
        }else{
            // 额外武器选择最低
            for(let i = 0;i < gg.data.user_team.length;i++){
                if(gg.data.user_team[i].order == TeamOrder[this.order - 1]){
                    for(let j = 0;j < gg.data.user_team[i].equips.length;j++){
                        base_lv = gg.data.user_team[i].equips[gg.data.user_team[i].hand]
                        break
                        // let lv = gg.data.user_team[i].equips[j]
                        // if(!base_lv){
                        //     base_lv = lv
                        //     continue
                        // }
                        // if(lv < base_lv){
                        //     base_lv = lv
                        // }
                    }
                }
            }
        }
        let tower_info = he.db.getDataByArgs("Equip",{ equip: info_list[0], level: base_lv})
        let role_info = he.db.getDataByArgs("Role", { roleId: info_list[1], level: base_lv })

        this.model.skeletonData = he.load.get(role_info.model,sp.SkeletonData,'role')
        this.reScript(tower_info.script)
        this.model_node.active = true // 这里改一手发光登场动画


        let user_out_status = false
        for(let i = 0;i < gg.data.user_team.length;i++){
            if(gg.data.user_team[i].order == TeamOrder[this.order - 1] && gg.data.user_team[i].id == role_info.roleId){
                user_out_status = true
            }
        }
        if(user_out_status){
            this.out_status = false
        }

        if(this.tower_script || this.controling){
            this.tower_script.initInfo(tower_info,role_info)
            if(user_out_status){
                this.tower_script.IdelAction(true)
            }else{
                this.changeAni(tower_info.pre + '_attack_before')
            }
        }else{
            he.error(tower_info.script,"脚本未绑定")
        }
        // 当前id , 编号
    },
    // 改变动画
    changeAni(ani,force){
        if(force){
          this.lastAni = ''
        }
        if(this.lastAni == ani || !ani){
            return 
        }
        this.lastAni = ani
        let breakS = ani.indexOf('_')
        let aniType = ani
        if(breakS >= 0){
            aniType = ani.substring(breakS + 1,ani.length)
        }
        switch(aniType){
            case 'attack_before':
                this.model.setAnimation(0,ani,false)
                break
            default:
                this.model.setAnimation(0,ani,true)
                break
        }
    },
    // 控制向前走一步
    chooseRole(){
        gg.control.roleUp(TeamOrder[this.order - 1])
    },
    handTurnAttack(pos){
        this.tower_script.turnDir(pos)
    },
    stopAttack(){
        if(this.tower_script){
            this.tower_script.IdelAction()
        }
    },
    // 获取世界坐标
    getWorldPos(){
        let worldPos = this.node.convertToWorldSpaceAR(cc.Vec2.ZERO)
        return cc.v2(parseInt(worldPos.x),parseInt(worldPos.y))
    },
    // 向前走一步
    forwardRole(){
        this.controling = true
        this.model_node.y = 50
        this.tower_script.IdelAction()
    },
    // 向后走一步
    backRole(){
        // 头调整
        this.controling = false
        this.model_node.y = 0
    },
    // 绑定脚本
    reScript(script){
        if(this.lastScript && this.lastScript != script){
            let cle = this.node.getComponent(this.lastScript)
            cle.clearInfo()
            this.node.removeComponent(this.lastScript)
            this.lastScript = null
        }
        if(script){
            this.lastScript = script
            let towerScript = this.node.getComponent(this.lastScript)
            if(towerScript){
                this.tower_script = towerScript
                return towerScript
            }else{
                this.tower_script = this.node.addComponent(script)
                return this.tower_script
            }
        }
    }
});
